#include "board.h"
#include "boardscene.h"
#include "settings.h"
#include <QString>
#include <QMessageBox>
#include <QFile>
//#include <cassert>

Board::Board(QWidget *parent)
    : QGraphicsView(parent)
{
    ifShowIndex = false;
    createScene();
}

void Board::setShowGrid(bool b)
{
    if(b)
    {
        scene->createGrid();
    }
    else scene->deleteGrid();
}

void Board::setShowIndex(bool b)
{
    if(b)
    {
        ifShowIndex = true;
        emit passIfShowIndex(true);
        scene->showIndex();
    }
    else
    {
        ifShowIndex = false;
        emit passIfShowIndex(false);
        scene->hideIndex();
    }
}

void Board::createScene()
{
    scene = new BoardScene();
    scene->setSceneRect(0,0,800,600);
    this->setScene(scene);

    connect(scene, SIGNAL(changeButtonToFalse()), this, SIGNAL(changeButtonToFalse()));
    connect(scene, SIGNAL(openReview()), this, SIGNAL(openReview()));
    connect(scene, SIGNAL(closeReview()), this, SIGNAL(closeReview()));
    connect(this, SIGNAL(passIfShowIndex(bool)), scene, SLOT(changeIfShowIndex(bool)));
    connect(scene, SIGNAL(disableShowIndex()), this, SIGNAL(disableShowIndex()));
    connect(scene, SIGNAL(enableShowIndex()), this, SIGNAL(enableShowIndex()));
    connect(scene, SIGNAL(showJudgment(QString)), this, SLOT(showJudgment(QString)));
    connect(scene, SIGNAL(disableGameSettings()), this, SIGNAL(disableGameSettings()));
    connect(scene, SIGNAL(windowIsModified()), this, SIGNAL(windowIsModified()));
    connect(scene, SIGNAL(loadSucceed()), this, SIGNAL(loadSucceed()));
    connect(scene, SIGNAL(loadFailed()), this, SIGNAL(loadFailed()));

    update();
}

void Board::newGame()
{
    delete scene;
    createScene();

    emit passIfShowIndex(ifShowIndex);
    emit enableGameSettings();
}

/* all slots below are make connection between mainwindow and scene obj */
void Board::startReview()
{
    scene->startReview();
}

void Board::backToFirst()
{
    scene->backToFirst();
}

void Board::jumpBackward()
{
    scene->jumpBackward();
}

void Board::backward()
{
    scene->backward();
}

void Board::forward()
{
    scene->forward();
}

void Board::jumpForward()
{
    scene->jumpForward();
}

void Board::goToEnd()
{
    scene->goToEnd();
}

void Board::backToInit()
{
    scene->backToInit();
}

void Board::quitReview()
{
    scene->quitReview();
}

void Board::regret()
{
    scene->regret();
}

void Board::startJudgment()
{
    scene->startJudgment();
}

void Board::showJudgment(QString qstring)
{
    int r = QMessageBox::information(this, tr("Result of Judgment"), qstring);

    if(r == QMessageBox::Ok) scene->deleteAllMoyos();
}

void Board::pass()
{
    scene->pass();
}

void Board::aiMove()
{
    scene->aiMove();
}

/* 开始新的让子局 */
void Board::changeHandicaps(int i)
{
    scene->changeHandicaps(i);
}

void Board::outputSGF(QFile* file, Settings *gameSettings)
{
    scene->outputSGF(file, gameSettings);
}

void Board::inputSGF(QFile *file, Settings *gameSettings)
{
    scene->inputSGF(file, gameSettings);
}

void Board::output2DBoard(const char* fileName)
{
    scene->output2DBoard(fileName);
}

